package com.hzy.fivepalace.db.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hzy.fivepalace.db.bean.DayStatis;
import com.hzy.fivepalace.db.dao.DtsUserAccountMapper;
import com.hzy.fivepalace.db.dao.DtsUserMapper;
import com.hzy.fivepalace.db.dao.ex.StatMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.hzy.fivepalace.db.domain.DtsUser;
import com.hzy.fivepalace.db.domain.DtsUserAccount;
import com.hzy.fivepalace.db.domain.DtsUserAccountExample;
import com.hzy.fivepalace.db.domain.DtsUserExample;
import com.hzy.fivepalace.db.domain.UserVo;

@Service
public class DtsUserService {

    @Resource
    private DtsUserMapper userMapper;

    @Resource
    private DtsUserAccountMapper userAccountMapper;

    @Resource
    private StatMapper statMapper;

    public DtsUser findById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * 获取用户昵称和手机号和头像
     *
     * @param userId
     * @return
     */
    public Map<String, Object> getUserNickNameAndPhoneAndAvatar(Integer userId) {
        Map<String, Object> info = new HashMap<>();
        String mobile = null;
        String avatar = null;
        String nickname = null;
        DtsUser user = findById(userId);
        if (null != user) {
            nickname = user.getNickname();
            mobile = user.getMobile();
            avatar = user.getAvatar();
        }
        info.put("avatar", avatar);//头像
        info.put("nickname", nickname);//昵称
        info.put("mobile", mobile);//手机号
        return info;
    }

    public UserVo findUserVoById(Integer userId) {
        DtsUser user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public DtsUser queryByOid(String openId) {
        DtsUserExample example = new DtsUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);
    }

    public DtsUser selectById(Integer userId) {
        DtsUserExample example = new DtsUserExample();
        example.createCriteria().andIdEqualTo(userId);
        return userMapper.selectOneByExample(example);
    }

    public void add(DtsUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    /**
     * 根据手机号查询用户
     *
     * @param phone
     * @return
     */
    public DtsUser queryByPhone(String phone) {
        DtsUserExample example = new DtsUserExample();
        example.orderBy("add_time desc");
        example.createCriteria().andMobileEqualTo(phone);
        return userMapper.selectOneByExample(example);
    }

    /**
     * 根据推荐码查询
     * @param RecodeCode
     * @return
     */
    public DtsUser queryByRecodeCode(String RecodeCode) {
        DtsUserExample example = new DtsUserExample();
        example.orderBy("add_time desc");
        example.createCriteria().andRecodeCodeEqualTo(RecodeCode);
        return userMapper.selectOneByExample(example);
    }

    public int updateById(DtsUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public List<DtsUser> querySelective(String username, String mobile, Integer page, Integer size, String sort,
                                        String order) {
        DtsUserExample example = new DtsUserExample();
        DtsUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andNicknameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);
    }

    public int count() {
        DtsUserExample example = new DtsUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);
    }

    public List<DtsUser> queryByUsername(String username) {
        DtsUserExample example = new DtsUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public boolean checkByUsername(String username) {
        DtsUserExample example = new DtsUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    public List<DtsUser> queryByMobile(String mobile) {
        DtsUserExample example = new DtsUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<DtsUser> queryByOpenid(String openid) {
        DtsUserExample example = new DtsUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        userMapper.logicalDeleteByPrimaryKey(id);
    }

    /**
     * 审批代理申请
     *
     * @param userAccount
     */
    public void approveAgency(Integer userId, Integer settlementRate, String shareUrl) {
        //获取账户数据
        DtsUserAccountExample example = new DtsUserAccountExample();
        example.or().andUserIdEqualTo(userId);

        DtsUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
        if (dbAccount == null) {
            throw new RuntimeException("申请账户不存在");
        }
        dbAccount.setShareUrl(shareUrl);
        if (!StringUtils.isEmpty(settlementRate)) {
            dbAccount.setSettlementRate(settlementRate);
        }
        dbAccount.setModifyTime(LocalDateTime.now());
        userAccountMapper.updateByPrimaryKey(dbAccount);

        //更新会员状态和类型
        DtsUser user = findById(userId);
        user.setUserLevel((byte) 2);//区域代理用户
        user.setStatus((byte) 0);//正常状态
        updateById(user);

    }

    public DtsUserAccount detailApproveByUserId(Integer userId) {
        // 获取账户数据
        DtsUserAccountExample example = new DtsUserAccountExample();
        example.or().andUserIdEqualTo(userId);

        DtsUserAccount dbAccount = userAccountMapper.selectOneByExample(example);
        return dbAccount;
    }

    public List<DtsUser> queryDtsUserListByNickname(String username, String mobile) {
        DtsUserExample example = new DtsUserExample();
        DtsUserExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)) {
            criteria.andNicknameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<DayStatis> recentCount(int statisDaysRang) {
        return statMapper.statisIncreaseUserCnt(statisDaysRang);
    }
}
